#!/bin/bash
#
#  File      : undel
#  Author    : cnscn
#  EMail     : 2003ysy@126.com
#  URL       : http://cnsdel.eliee.com
#  QQ        : 214363570
#  Time      : 2005-08-28 18:00 / 2004-11-30 14:26
#  Copyright : Cnsdel is free software, covered by the GNU General Public License
#              And you are welcome to change it and/or distribute copies of it under certain conditions
#


source  $(dirname $0)/com
source  $(dirname $0)/argsmenufile

if [ $# -eq 0 ] ; then
        echo "Too few arguments"

        echo "undel -h for more information"
        exit  1
fi


#########################::BGN the function undel  NGB::#####################################

undelExec()
{

for i in $*
do

        if [ "$i"  != "$1" ]
        then
            continue
        fi

        
        #get the file name 
                
        cd ~/.trash
 
        if [ ! -f $i  -a  ! -d $i ]
        then
            if [ ${i:${#i}-1:1} != "*" ]
            then
                echo "$i  not  in the .trash!";
            fi
            continue
        fi
        

        if [ -d "$i" ]
        then 
            parentpath=$(dirname $( echo "$i" |  tr "=" "/" ) ) 2>del.err
        else
            parentpath=$(dirname $( echo "$i" | tr "="  "/" ) ) 2>del.err
        fi
        
        f_name=$(basename $( echo "$i" | tr "=" "/" ) ) 2>del.err
        
        if [ !  -d  $parentpath ]
        then 
            echo  -n "Directory $parentpath does not exist, create it (yY|nN) : "
             read  answer
             case $answer in
                 y|Y)
                     mkdir -p  $parentpath
                     ;;
                 *)
                     exit
             esac
  
        fi       
   
        mv -b "$i"  "$(echo $parentpath)/$f_name" 
        
        cd $currpath
	
        echo -n "${i} recover successfully... ["

        funcTput green_black
            echo -n  "OK"
        funcTput endtput

        echo "]"     


done

}

#########################::END the function undel  DNE::#####################################



####### BNG search all files in dir which like lib/*#########

for i in $*
do
    if [ -f $i ]
	then 
        echo "a file  $i"
	fi
        
    fullpath=$(getPath $currpath $i) 
            
    tmpdir=$(dirname $fullpath )
    
    fileintrash=$(echo "$fullpath" | tr "/" "=" )
    trashfile="$HOME/.trash/$fileintrash";
          
    if [ "${i:${#i}-1:1}" != "*"  -a  ! -f "$trashfile"  -a ! -d "$trashfile" ]
    then
 
    echo -n "no such file  "

    funcTput red_black
    tput bold
    echo -n $fileintrash 
    funcTput endtput

    echo " in trash"
        
    fi

	for k in $(ls ~/.trash)
        do
           tmp2dir=$(echo  "$k" | tr "=" "/")
          
           export filetrash  
           
	   if [ -f  "$fullpath" ]
	   then
		 if [  "$fileintrash" == "$k" ]
		 then
		   undelExec  $k
		   cd  $tmpdir
		 fi
		 
	   elif [  "${tmpdir}" == "$(dirname ${tmp2dir})"  ]
	   then 
                  undelExec  $k 
                  cd $tmpdir
           fi
	
        done

done

####### END search all files in dir which like lib/* #########
